#!/bin/bash
currentcpuload=`uptime | awk -F"load average:" '{print $NF}'|awk -F" " '{print $1}'|awk -F"." '{print $1}'`
if [ $currentcpuload -ge 100 ];then
    exit 
fi
script=$(cd `dirname $0`; pwd)"/"$(basename $0)
#runnum=`ps aux | grep $script | wc -l`
#if [ $runnum -gt 3 ];then
#exit
#fi
date=`date +%s`
day=`date +%d`
dirpath="/dev/shm/omat/nodestat/"
if [ ! -d $dirpath ]; then
    mkdir -p $dirpath
fi
filtertime='12:00:[0-9][0-9] AM'
restartmsg=""
timetmp=`sar -A -f /var/log/sa/sa$day | grep -A2 RESTART | tail -n 1 | grep CPU| awk '{print $1" "$2}'`
rebooted=`sar -A -f /var/log/sa/sa$day | grep -A2 RESTART | tail -n 1 `
if [ "$rebooted" != "" ];then
if [ "$timetmp" != "" ];then
filtertime=$timetmp
restartmsg=`sar -A -f /var/log/sa/sa$day | grep -A2 RESTART |tail -n 3 | xargs -n 1 | tr -t "\n" " "`
else
datemsg=`sar -A -f /var/log/sa/sa$day | grep Linux`
echo $rebooted "NODEISREBOOTED" $datemsg > $dirpath"sar_"$date".data"
#echo "last time:"$objstr
#echo "label time:"$filtertime
exit
fi
fi
objstr=`sar -A -f /var/log/sa/sa$day | grep -B1 Average|tail -n 2 | grep -v Average|awk '{print $1" "$2}'`
#echo "last time:"$objstr
#echo "label time:"$filtertime
msg=`sar -A -f /var/log/sa/sa$day | grep "$objstr\|Linux\|$filtertime"`
#if [ "$restartmsg" == "" ];then
echo $msg > $dirpath"sar_"$date".data"
#else
    #echo $restartmsg "NODEISREBOOTED" $msg > $dirpath"sar_"$date".data"
#fi
#nvidia-smi stats -d pwrDraw,temp,memUtil,gpuUtil -c 1 -f $dirpath"gpumon_"$date".data"
find $dirpath -cmin +1200 -name "sar_*"  -exec /bin/rm -rf {} \;
